<template>
    <view class="navbar">
        <view class="navbar__back" @click="handleBack">
            <u-icon name="arrow-left" size="48" customPrefix="uicon"></u-icon>
        </view>
        <view class="navbar__title">切换店铺</view>
    </view>
    <view class="current">
        <image :src="currentShopStore.logo" class="current__logo" />
        <view class="current__info">
            <text class="current__info--name">{{ currentShopStore.name }}</text>
            <text class="current__info--mode">{{ SHOP_MODE[currentShopStore.mode as keyof typeof SHOP_MODE] }}</text>
        </view>
        <view class="current__login">当前登录</view>
    </view>
    <block v-if="enableShopList.length">
        <view class="enable">可切换</view>
        <view class="list">
            <view class="list__container">
                <view
                    v-for="enableShop in enableShopList"
                    :key="enableShop.id"
                    class="list__container--item change"
                    @click="checkedShopId = enableShop.id"
                >
                    <image :src="enableShop.logo" class="change__logo" />
                    <view class="change__info" :class="{ checked: checkedShopId === enableShop.id }">
                        <text class="change__info--name">{{ enableShop.name }}</text>
                        <text class="change__info--mode">{{ SHOP_MODE[enableShop.mode as keyof typeof SHOP_MODE] }}</text>
                    </view>
                </view>
            </view>
        </view>
        <view class="bottom">
            <view class="bottom__btn" :class="{ disabled: !checkedShopId }" @click="handleChangeShop">确认切换</view>
        </view>
    </block>
    <div v-else class="enable">暂无可切换店铺</div>
</template>

<script lang="ts" setup>
import { computed, ref } from 'vue'
import { doGetEnableChangeShop, signByUser } from '@/apis/sign/index'
import { useShopInfoStore } from '@/store/modules/shopInfo'
import { ShopInfoStore } from '@/store/modules/shopInfo/state'
import { GrantType } from '@/apis/sign/type'
import { onLoad } from '@dcloudio/uni-app'

onLoad(() => {
    uni.$emit('updateTitle')
})
const shopStore = useShopInfoStore().getterShopInfo
const currentShopStore = computed(() => shopStore)
enum SHOP_MODE {
    B2B2C = '线上店铺',
    O2O = 'O2O店铺',
}
const checkedShopId = ref<string | null>(null)
const handleBack = () => {
    uni.navigateBack()
}
const enableShopList = ref<ShopInfoStore[]>([])
const initialEnableShopList = async () => {
    const result = await doGetEnableChangeShop()
    if (result.code === 200 && result.data?.length) {
        enableShopList.value = result.data
    }
}
initialEnableShopList()
const handleChangeShop = async () => {
    if (!checkedShopId.value) return
    const result = await signByUser(GrantType.SWITCH_SHOP, { shopId: checkedShopId.value })
    if (result.code === 200) {
        const chooseShop = enableShopList.value.find((enableShop) => enableShop.id === checkedShopId.value)
        if (chooseShop) {
            useShopInfoStore().SET_SHOP_INFO(chooseShop)
        }
        const { value, refreshToken } = result.data
        const access_token = value
        const refresh_token = refreshToken?.value
        useShopInfoStore().SET_SHOP_TOKEN({ access_token, refresh_token })
        uni.showToast({ title: '切换店铺成功', icon: 'none' })
        uni.reLaunch({ url: '/pages/platform/index' })
    }
}
</script>
<style lang="scss" scoped>
@include b(navbar) {
    position: fixed;
    top: var(--status-bar-height);
    z-index: 99;
    left: 0;
    right: 0;
    background-color: #fff;
    display: flex;
    height: calc(90rpx);
    @include e(back) {
        width: 90rpx;
        height: 90rpx;
        border-radius: 20rpx;
        background-color: rgba(255, 255, 255, 1);
        margin-left: 16rpx;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    @include e(title) {
        text-align: center;
        height: calc(90rpx + var(--status-bar-height));
        line-height: calc(90rpx + var(--status-bar-height));
        position: absolute;
        top: 0;
        left: 112rpx;
        right: 112rpx;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        font-weight: bold;
    }

    @include b(back) {
        position: absolute;
        left: 0;
        top: 0;
    }
}
@include b(current) {
    margin-top: calc(90rpx + var(--status-bar-height));
    padding: 30rpx;
    border-bottom: 1rpx solid rgba(0, 0, 0, 0.1);
    display: flex;
    background-color: #fff;
    border-top: 1rpx solid rgba(0, 0, 0, 0.1);
    justify-content: space-between;
    @include e(logo) {
        width: 110rpx;
        height: 110rpx;
        border-radius: 50%;
        flex-shrink: 0;
    }
    @include e(info) {
        flex: 1;
        margin-left: 20rpx;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        height: 110rpx;
        @include m(name) {
            color: #222;
            font-weight: 500;
            font-size: 32rpx;
        }
        @include m(mode) {
            color: #666;
            font-size: 30rpx;
        }
    }
    @include e(login) {
        font-size: 30rpx;
        color: #222;
    }
}
@include b(enable) {
    padding: 30rpx 0 16rpx 30rpx;
    font-size: 30rpx;
    line-height: 42rpx;
    color: #333;
}
@include b(list) {
    padding: 0 20rpx;
    @include e(container) {
        background-color: #fff;
        box-shadow: 0px 0px 15px -3px rgba(0, 0, 0, 0.25);
        border-radius: 20rpx;
        padding: 25rpx 20rpx;
        @include b(change) {
            display: flex;
            justify-content: space-between;
            @include e(logo) {
                width: 110rpx;
                height: 110rpx;
                border-radius: 50%;
                flex-shrink: 0;
            }
            @include e(info) {
                flex: 1;
                margin-left: 20rpx;
                display: flex;
                flex-direction: column;
                justify-content: space-between;
                height: 110rpx;
                @include m(name) {
                    color: #222;
                    font-weight: 500;
                    font-size: 32rpx;
                }
                @include m(mode) {
                    color: #666;
                    font-size: 30rpx;
                }
            }
        }
        .change + .change {
            margin-top: 37rpx;
        }
        .checked {
            .change__info--name {
                color: #f00 !important;
            }
            .change__info--mode {
                color: #f00 !important;
            }
        }
    }
}
@include b(bottom) {
    position: fixed;
    bottom: env(safe-area-inset-bottom);
    left: 0;
    right: 0;
    @include e(btn) {
        height: 90rpx;
        background-color: #4755e9;
        line-height: 90rpx;
        text-align: center;
        font-size: 34rpx;
        color: #fff;
    }
    .disabled {
        background-color: #bed4ff;
    }
}
</style>
